package com.Rolin.wss.service.impl;

import com.Rolin.wss.dao.FlightMapper;
import com.Rolin.wss.model.*;
import com.Rolin.wss.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class FlightServiceImpl implements FlightService {
    //注入DAO
    @Autowired
    FlightMapper flightMapper;

    @Override
    public ArrayList<FlightBO> getFlightInfo(int isFly) {
        ArrayList<FlightBO> flightBOs = new ArrayList<FlightBO>();
        switch (isFly){
            case 2: //已经起飞的飞机
                flightBOs = typeToFlight(flightMapper.selectFlightsData(1));
                break;
            case 1:
                flightBOs = typeToFlight(flightMapper.selectFlightsData(0));
                break;
            case 0:
                flightBOs.addAll(typeToFlight(flightMapper.selectFlightsData(0)));
                flightBOs.addAll(typeToFlight(flightMapper.selectFlightsData(1)));
                break;
        }
        return flightBOs;
    }



    private ArrayList<FlightBO> typeToFlight(ArrayList<FlightVO> flightVOs){
        ArrayList<FlightBO> flightBOs = new ArrayList<FlightBO>();
        for (FlightVO flightVO : flightVOs) {
            FlightTypeVO type = flightMapper.selectAirplaneType(flightVO.getFlight_type_id());
            FlightBO fbo = new FlightBO(flightVO.getFlight_id(),
                    "WSS-"+flightVO.getFlight_no(),
                    type,
                    flightVO.getFlight_start(),
                    flightVO.getFlight_end(),
                    flightVO.getFlight_time(),
                    flightVO.getFlight_price(),
                    flightVO.getFlight_isfly());
            flightBOs.add(fbo);
        }

        return flightBOs;
    }

    @Override
    public int havePurchasedTicket(int flightId, int userId) {
        FlightOrderVO fovo = flightMapper.selectFlightOrderByFlightUser(flightId,userId);
        if(fovo == null) return -1;
        else return fovo.getFo_id();
    }

    public boolean buyTicket(int flightId,int userId){
        String ticketNo = Long.toString(System.currentTimeMillis());
        Date date = new Date();// this object contains the current date value
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String ticketDate = formatter.format(date);

        FlightOrderVO fovo = new FlightOrderVO(null,ticketNo,flightId,userId,ticketDate);

        int result = flightMapper.insertFlightOrderByFlightUser(fovo);

        return result == 1;
    }

    @Override
    public FlightPOJO getTicketInfo(int ticket) {
        FlightOrderVO fo = flightMapper.selectFlightOrderByTicketID(ticket);
        FlightVO fv = flightMapper.selectFlightByID(fo.getFo_flight());
        FlightTypeVO ft = flightMapper.selectAirplaneType(fv.getFlight_type_id());
        FlightBO fb = new FlightBO(fv.getFlight_id(),
                "WSS-"+fv.getFlight_no(),
                ft,
                fv.getFlight_start(),
                fv.getFlight_end(),
                fv.getFlight_time(),
                fv.getFlight_price(),
                fv.getFlight_isfly());
        FlightPOJO pojo = new FlightPOJO(fo.getFo_id(),
                fo.getFo_no(),
                fb,
                fo.getFo_data());

        return pojo;
    }
}
